package com.mrd.gtimp.business.action;

import com.mrd.constant.MsgConstants;
import com.mrd.exception.QueryException;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.framework.ui.filter.Filter;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.framework.utils.HttpUtils;
import com.mrd.gtimp.base.entity.User;
import com.mrd.gtimp.business.entity.RejectContent;
import com.mrd.gtimp.business.service.BookingBiz;
import com.mrd.gtimp.business.service.DeclarationBiz;
import com.mrd.gtimp.business.service.RejectContentBiz;
import com.mrd.gtimp.business.service.TrailBiz;
import com.mrd.util.ModelPrinter;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

import javax.annotation.Resource;


@Controller
@RequestMapping("/business")
public class RejectContentAction {
    private static final long serialVersionUID = 1L;
    @Resource
    private RejectContentBiz rejectContentBiz;
    @Resource
    private BookingBiz bookingBiz;
    @Resource
    private TrailBiz trailBiz;
    @Resource
    private DeclarationBiz declarationBiz;

    /**
     * 添加驳回内容
     */
    @RequestMapping("/rejectContent-reject")
    public String reject() {
        String id = HttpUtils.getParam("id");
        HttpUtils.getRequest().setAttribute("id", id);
        String type = HttpUtils.getParam("type");
        HttpUtils.getRequest().setAttribute("type", type);
        return "/gtimp/rejectContent/reject";
    }

    /**
     * 编辑信息
     */
    @RequestMapping("/rejectContent-find")
    public String find(RejectContent rejectContent) {
        if (null != rejectContent) {
            if (StringUtils.isNotEmpty(rejectContent.getId())) {
                try {
                    rejectContent = rejectContentBiz.findEntityById(rejectContent.getId() + "");
                } catch (QueryException e) {
                    e.printStackTrace();
                }
            }
        }
        ModelPrinter.print1(rejectContent);
        return null;
    }

    /**
     * 保存新增修改信息
     */
    @RequestMapping("/rejectContent-saveOrUpdate")
    @ResponseBody
    public Result saveOrUpdate(User user, RejectContent rejectContent) {
        Result msg;
        String id = HttpUtils.getParam("id");
        String type = HttpUtils.getParam("type");
        try {
            if (StringUtils.isEmpty(rejectContent.getId())) {
                rejectContent.setId(null);
                rejectContent.setRejectDate(new Date());
                rejectContent.setRejectPerson(user.getName());
                rejectContent.setDeclaration(declarationBiz.findEntityById(id));
            }
            rejectContentBiz.saveOrupdateEntity(rejectContent);
            if (type.equals("1")) {// 报关
                declarationBiz.updateValidateReject(id);
            } else if (type.equals("2")) {// 拖车
                trailBiz.updateValidateReject(id);
            } else if (type.equals("3")) {// 订舱
                bookingBiz.updateValidateReject(id);
            }

            msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES);
        } catch (Exception e) {
            e.printStackTrace();
            msg = Result.newFailInstance(MsgConstants.SAVE_FAIL);
        }
        return msg;
    }

    /**
     * 查看驳回内容列表
     */
    @RequestMapping("/rejectContent-rejectList")
    public String rejectList() {
        String id = HttpUtils.getParam("id");
        try {
            Filter filters = new Filter();
            SearchRule rule = new SearchRule("bid", SearchRuleOption.eq, id);
            filters.addRule(rule);
            // Order order = new Order();
            // if(null != sidx && !"".equals(sidx)) {
            // order.add(sidx, sord);
            // }
//            rejectContents = rejectContentBiz.findByJqgrid(0, 0, filters, null);
            Pageable page = new PageRequest(0, 0, Sort.Direction.DESC);
            Jqgrid jqgrid = new Jqgrid(filters, page);
            Page<RejectContent> rejectContents = rejectContentBiz.findByJqgrid(jqgrid);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/gtimp/rejectContent/rejectContent";
    }

    public BookingBiz getBookingBiz() {
        return bookingBiz;
    }

    public void setBookingBiz(BookingBiz bookingBiz) {
        this.bookingBiz = bookingBiz;
    }

    public RejectContentBiz getRejectContentBiz() {
        return rejectContentBiz;
    }

    public void setRejectContentBiz(RejectContentBiz rejectContentBiz) {
        this.rejectContentBiz = rejectContentBiz;
    }

    public DeclarationBiz getDeclarationBiz() {
        return declarationBiz;
    }

    public void setDeclarationBiz(DeclarationBiz declarationBiz) {
        this.declarationBiz = declarationBiz;
    }

}
